<template>
	<view class="deploy">
		<view class="form form-a">
			<view class="form-group">
				<view class="form-item">
					<text class="form-item-header">任务名称：</text>
					<text class="form-item-body"> {{ info.name }} </text>
				</view>
				<view class="form-item">
					<text class="form-item-header">发布时间：</text>
					<text class="form-item-body">{{ info.releaseTime }}</text>
				</view>
				<view class="form-item">
					<text class="form-item-header">部署事项：</text>
					<text class="form-item-body"> {{ info.contents }} </text>
				</view>
				<view class="form-item">
					<text class="form-item-header">发布人：</text>
					<text class="form-item-body">{{ info.releaseUser }}</text>
				</view>
				<view class="form-item">
					<text class="form-item-header uni-link">附件：</text>
					<text class="form-item-body">{{ info.filePath }}</text>
				</view>
			</view>
		</view>

		<div class="list">
			<div class="list-group">
				<view class="list-item" v-for="(item, index) in list" :key="index">
					<view class="list-item-header">{{ index + 1 }}</view>
					<view class="list-item-body">
						<view class="card" :class="{'card-a': item.srcLevel == 1, 'card-b': item.srcLevel == 2 && item.deptCode == 500000}">
							<view class="card-body" :class="{'text-alert':statusAfter == 0 && type == 'middle' && !(item.srcLevel == 2 && item.deptCode == 500000)}">{{ item.name }}
								<image v-if="item.srcLevel == 1" class="icon" src="@/static/icon/iv_city_icon.png" mode="aspectFit"></image>
								<image v-if="item.srcLevel == 2 && item.deptCode == 500000" class="icon" src="@/static/icon/gx.png" mode="aspectFit"></image>
							</view>
						</view>
					</view>
					<view class="list-item-toolbar btns">
						<text v-if="(statusAfter == 0 && type == 'middle') || (statusAfter == 0 && type == 'before' && item.isReport == '0')" class="btn" @tap="report(item)">上报</text>
						<text v-else class="btn" @tap="viewDetail(item)">查看</text>
					</view>
				</view>
			</div>
		</div>
	</view>
</template>
<script>
	import { querySzbmJsdBusinessMiddle, querySzbmJsdBusiness, queryMiddleJsdList, queryCheckIsReport} from '@/api/configure.js';
	import { deepClone, mergeUrls, getUserInfo, formatDateStr } from '@/util/util.js';
	import { pathToBase64, base64ToPath } from '@/util/mmmm-image-tools/index.js';
	import configUrl from '@/util/url.js';
	export default {
		data() {
			return {
				type: '',
				title: '',
				types: [{
					value: 'before',
					label: '预警部署',
				}, {
					value: 'middle',
					label: '任务部署'
				}],
				userInfo: {},
				info: {},
				list: [],
				statusAfter: 1,
				id: ''
			}
		},
		methods: {
			init() {
				uni.$on('floodControl.deploy.checkJsd', this.checkJsd)

				this.userInfo = getUserInfo();

				this.getData();
			},
			async checkJsd(data) {
				let id = data.jsdID;
				let isReport = null;

				let params = {
					businessId: this.id,
					jsdId: id
				}

				let res = await this.getCheckIsReport(params);

				if (res && res.code == 0) {
					isReport = res.data;
				}

				this.list.forEach((item) => {
					if (item.id == id) {
						item.isReport = isReport;
					}
				})
			},
			getCheckIsReport(data = {
				businessId: '',
				jsdId: ''
			}) {
				return new Promise((resolve, reject) => {
					queryCheckIsReport(data).then((res) => {
						resolve(res);
					}, () => {
						reject();
					})
				})
			},

			async getData() {
				let data = {
					id: this.id
				};
				let list = [];
				let includeList = [];

				await queryMiddleJsdList({
					businessId: this.id
				}).then((res) => {
					list = res.data.jsdList.slice(0);
				})

				if (this.type == 'middle') {
					data.userName = this.userInfo.userId;

					await querySzbmJsdBusinessMiddle(data).then((res) => {
						this.info = deepClone(res.data.szbmJsdBusinessMiddle);

						includeList = res.data.szbmJsdBusiness.params.jsdList ? JSON.parse(res.data.szbmJsdBusiness.params.jsdList.value) : [];
					})
				} else {
					data.organizationId = this.userInfo.threeOrganizationId;

					await querySzbmJsdBusiness(data).then((res) => {
						this.info = deepClone(res.data);

						includeList = res.data.params.jsdList ? JSON.parse(res.data.params.jsdList.value) : [];
					})
				}


				if (this.statusAfter == '0') {
					if (this.type == 'before') {
						this.list = await Promise.all(list.map(async item => {
							let params = {
								businessId: this.id,
								jsdId: item.id
							}
							let data = 0;

							let res = await this.getCheckIsReport(params);

							if (res && res.code == 0) {
								data = res.data;
							}

							item.isReport = data;

							return item;

						}))
					} else {
						this.list = list.slice(0);
					}
				} else {
					this.list = list.filter((item) => {
						let isFindIndex = includeList.findIndex((ite) => {
							return item.id == ite.id;
						})

						return isFindIndex != -1;
					})
				}
			},
			render() {
				let type = this.types.find(type => type.value == this.type)

				this.title = type.label

				uni.setNavigationBarTitle({
					title: `${this.title}`
				});
			},
			viewDetail(item) {
				let data = {
					jsdId: item.id,
					businessId: this.id,
					name: item.name,
					address: item.address,
					jsdType: item.jsdType,
					type: this.type
				}

				let url = '';

				if (this.type == 'middle') {
					url = '/pages/floodControl/details/task';
				} else {
					url = '/pages/floodControl/details/warning';
				}

				url = mergeUrls(url, data);

				uni.navigateTo({
					url: url
				});
			},
			report(item) {
				let coordinates = JSON.parse(item.gcjGeometry)['coordinates']

				let data = {
					type: this.type,
					jsdId: item.id,
					businessId: this.id,
					name: item.name,
					address: item.address,
					jsdType: item.jsdType,
					gdx: coordinates[0],
					gdy: coordinates[1]
				}

				let url = '';

				if (this.type == 'before') {
					url = '/pages/floodControl/report/warning';
				} else {
					url = '/pages/floodControl/report/task';
				}

				url = mergeUrls(url, data);

				uni.navigateTo({
					url: url
				});
			}
		},
		onLoad(option) {
			this.type = option.type;
			this.id = option.id;
			this.statusAfter = option.statusAfter;

			this.init();
		},
		onUnload() {
			uni.$off('floodControl.deploy.checkJsd');
		},
		onReady() {
			this.render();
		}
	}
</script>

<style lang="scss">
	.text-alert {
		color: #dd524d;
	}

	.form {}

	.form-a {
		margin-top: 20rpx;
		color: #767676;

		.form-group {
			.form-item {
				padding: 6rpx 30rpx;
				margin-bottom: 8rpx;
				display: flex;

				.form-item-header {
					width: 140rpx;
					font-size: 28rpx;
					line-height: 120%;
				}

				.form-item-body {
					box-sizing: border-box;
					padding-left: 10rpx;
					width: calc(100% - 140rpx);
					font-size: 28rpx;
					line-height: 120%;
				}

				&.form-item-ver {
					flex-direction: column;

					.form-item-header {
						width: 100%;
						margin-bottom: 10rpx;
					}

					.form-item-body {
						width: 100%;
						padding: 20rpx;
						border-radius: 10rpx;
						line-height: 120%;
						background-color: #ececec;
					}
				}
			}


		}
	}

	.list {
		.list-group {
			position: relative;
			top: 0rpx;
			left: 0rpx;

			&:before {
				height: 1rpx;
				content: '';
				position: absolute;
				top: 0rpx;
				right: 0rpx;
				left: 0rpx;
				background-color: #bdbdbd;
				transform: scaleY(.5);
				opacity: 0.382;
			}

			.list-item {
				display: flex;
				font-size: 28rpx;
				line-height: 120%;
				color: #767676;
				text-align: center;
				position: relative;
				top: 0rpx;
				left: 0rpx;

				&:before {
					height: 1rpx;
					content: '';
					position: absolute;
					bottom: 0rpx;
					right: 0rpx;
					left: 0rpx;
					background-color: #bdbdbd;
					transform: scaleY(.5);
					opacity: 0.382;
				}

				.list-item-header {
					width: 100rpx;
					padding: 16rpx 10rpx;
					box-sizing: border-box;
				}

				.list-item-body {
					width: calc(100% - 220rpx);
					text-align: center;
					box-sizing: border-box;
					position: relative;
					top: 0rpx;
					left: 0rpx;

					&:before {
						width: 1rpx;
						content: '';
						position: absolute;
						top: 0rpx;
						right: 0rpx;
						bottom: 0rpx;
						background-color: #bdbdbd;
						transform: scaleX(.5);
						opacity: 0.382;
					}

					&:after {
						width: 1rpx;
						content: '';
						position: absolute;
						top: 0rpx;
						bottom: 0rpx;
						left: 0rpx;
						background-color: #bdbdbd;
						transform: scaleX(.5);
						opacity: 0.382;
					}
				}

				.list-item-toolbar {
					padding: 16rpx 10rpx;
					width: 120rpx;
					box-sizing: border-box;
				}
			}
		}
	}

	.card {
		padding: 16rpx 10rpx;

		.card-body {
			position: relative;
			top: 0rpx;
			left: 0rpx;

		}

		.icon {
			position: absolute;
			top: 0rpx;
			right: 0rpx;
		}

		&.card-a {
			.card-body {
				padding-right: 40rpx;
			}

			.icon {
				width: 32rpx;
				height: 32rpx;
			}
		}

		&.card-b {
			.card-body {
				padding-right: 120rpx;
			}

			.icon {
				width: 120rpx;
				height: 32rpx;
			}
		}
	}

	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
		background-color: #f29004;
	}
</style>
